package com.rw.coco.handler;

import com.rw.coco.api.data.CocoDataApi;
import com.rw.coco.api.modle.Account;
import com.rw.coco.api.modle.Bot;
import com.rw.coco.api.modle.Group;
import com.rw.coco.api.event.GroupMuteEvent;
import com.rw.tool.util.string.StringFormatter;

/**
 * 群禁言触发器
 */
public class GroupMuteHandler {
    private static final String MUTE_MSG = "{}({}) 在群 {}({}) 中，被 {}({}) 禁言";

    private final CocoDataApi dataApi;

    public GroupMuteHandler(CocoDataApi dataApi) {
        this.dataApi = dataApi;
    }

    public void handle(GroupMuteEvent groupMuteEvent) {
        Account account = groupMuteEvent.muteBy();
        Group group = groupMuteEvent.muteIn();
        Bot bot = groupMuteEvent.getBot();

        // 输出日志
        String info = StringFormatter.format(MUTE_MSG, bot.getName(), bot.getId(), group.getName(), group.getId(),
                account.getNick(), account.getId());
        dataApi.getCocoLogger().info(info);

        // 将禁言操作者与群加入黑名单
        dataApi.getPersistenceMapper().blackAccount(account.getId());
        dataApi.getPersistenceMapper().blackGroup(group.getId());

        // 退群删好友
        groupMuteEvent.deleteOperator();
        groupMuteEvent.quitGroup();
    }
}
